/* Copyright 2009 Requirements Management System 
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.mags.remas.view.commons;

import org.eclipse.jface.resource.ImageDescriptor;
import org.eclipse.swt.graphics.Image;
import org.eclipse.ui.plugin.AbstractUIPlugin;
import org.mags.remas.app.Activator;

import java.util.HashMap;
import java.util.Map;

/**
 * 
 * @author Mario Gaitan
 *
 */
public class ImageProvider {
	private static Map<String, Image> cache = new HashMap<String, Image>();
	public static Image getKeyImage(String image) {
		return getImage("icons/key/"+image);
	}
	public static ImageDescriptor getKeyImageDescriptor(String image) {
		return getImageDescriptor("icons/key/"+image);
	}
	public static Image getCommonImage(String image) {
		return getImage("icons/commons/"+image);
	}
	public static ImageDescriptor getCommonImageDescriptor(String image) {
		return getImageDescriptor("icons/commons/"+image);
	}	
	public static Image getEntityImage(String image) {
		return getImage("icons/entity/"+image);
	}
	public static Image getImage(String path) {
		String _path = checkExt(path);
		Image image = cache.get(_path);
		if(image==null) {
			ImageDescriptor imageDescriptor = AbstractUIPlugin.imageDescriptorFromPlugin(Activator.PLUGIN_ID, _path);
			if(imageDescriptor!=null) { 
				image = imageDescriptor.createImage();
				cache.put(_path, image);
			}
		}
		return image;
	}
	public static String checkExt(String path) {
		if(!path.endsWith(".png"))
			return path.concat(".png");
		return path;
	}
	public static ImageDescriptor getEntityImageDescriptor(String image) {
		return getImageDescriptor("icons/entity/"+image);
	}	
	public static ImageDescriptor getImageDescriptor(String path) {
		String _path = checkExt(path);
		return AbstractUIPlugin.imageDescriptorFromPlugin(Activator.PLUGIN_ID, _path);
	}
}
